﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lg.Core.Extensions;
using Lg.Core.Exceptions;

namespace Lg.Core.Form.Types
{
    public class MultipleSelect:BaseType,IType
    {
        /// <summary>
        /// 前端以字符串数组的方式提交
        /// </summary>
        public List<Option>? Options { get; set; }
        public bool OtherValueRequired { get; set; }
        /// <summary>
        /// 其它附加值
        /// </summary>
        public string? OtherValue { get; set; }
        /// <summary>
        /// 使用图标选项
        /// </summary>
        public bool IconOptions { get; set; }

        public void Verify()
        {
            if (this.IsRender)
            {
                if (this.Required)
                {
                    if (!string.IsNullOrEmpty(this.Value))
                    {
                        var values=this.Value.Split(',');
                        if (!values.Any())
                        {
                            throw new Exceptions.LgException($"[{this.Title}]为必选项，请至少选择一项！");
                        }
                    }
                    else
                    {
                        throw new Exceptions.LgException($"[{this.Title}]为必选项，请至少选择一项！");
                    }
                    
                }
                
            }
        }
        public override FormData? SetValue(JObject formData)
        {
            string? value = formData[this.Name!]?.ToString()??"";
            if (!string.IsNullOrEmpty(value))
            {
                if (value == "lg-no-render")
                {
                    this.IsRender = false;
                    value = "";
                }
                else
                {
                    List<OptionValue>? values = value.ToList<OptionValue>();
                    if (values != null&&values.Count>0)
                    {
                        value = string.Join(',', values.Select(p => p.value));
                    }
                }
            }
            this.Value = value;
            return this.GetValue();
        }

        public void GetSaveDbValue(Dictionary<string, dynamic> formData, string inputValue)
        {
            if (string.IsNullOrEmpty(inputValue))
            {
                formData.Add(key: Name, inputValue);
            }
            else
            {
                var values = inputValue.Split('~');
                if (values.Any())
                {
                    foreach (var item in values)
                    {
                        var op = Options?.FirstOrDefault(x=>x.Text==item);
                        if (op!=null)
                        {
                            formData.Add(Name, new { opid = op.Id, value = item });
                        }
                        else
                        {
                            formData.Add(Name, new { opid = 0, value = item });
                        }
                    }
                }
                else
                {
                    throw new LgException($"{Title}是多选,请用[~]隔开");
                }
            }

        }
    }
}
